import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop_z/model/commodity.dart';
import 'package:flutter_shop_z/model/commodity_cart.dart';
import 'package:flutter_shop_z/pages/cart_page/bloc/cart_bloc.dart';
import 'package:flutter_shop_z/pages/index_page/bloc/index_count_bloc.dart';
import 'package:flutter_shop_z/shared_preferences/commodity_cart_prefs.dart';
import 'package:shared_preferences/shared_preferences.dart';

class DetailsBottom extends StatelessWidget {
  Commodity commodity;
  DetailsBottom(this.commodity);
  @override
  Widget build(BuildContext context) {
    return Container(
      width: ScreenUtil().setWidth(750),
      color: Colors.white,
      height: ScreenUtil().setHeight(80),
      child: Row(
        children: <Widget>[
          _commodityCart(context),
          _addCart(context),
          _nowBuy(context),
        ],
      ),
    );
  }

//购物车
  Widget _commodityCart(context) {
    return BlocBuilder<CartBloc, CartState>(builder: (context, state) {
      return InkWell(
        onTap: () {
          print("购物车");
          BlocProvider.of<IndexCountBloc>(context).add(SetIndexCountEvent(2));
          Navigator.pop(context);
        },
        child: Container(
          width: ScreenUtil().setWidth(110),
          alignment: Alignment.center,
          child: Stack(children: [
            Icon(
              Icons.shopping_cart,
              size: 35,
              color: Colors.purple,
            ),
            Positioned(
              child: Container(
                // padding: EdgeInsets.all(1),
                padding: EdgeInsets.fromLTRB(6, 3, 6, 3),
                decoration: BoxDecoration(
                  color: Colors.pink,
                  border: Border.all(width: 1, color: Colors.white),
                  borderRadius: BorderRadius.circular(12.0),
                ),
                child: Text(
                  '${_cartNum(state.listCartStr)}',
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(18),
                    color: Colors.white,
                    backgroundColor: Colors.red,
                  ),
                ),
              ),
              top: 0,
              right: 0,
            )
          ]),
        ),
      );
    });
  }

  int _cartNum(String listStr) {
    int num = 0;
    List<CommodityCart> list = CommodityCartPrefs.decodeList(listStr);
    list.forEach((element) {
      if (element.isCheck) {
        num += element.number;
      }
    });
    return num;
  }

  //加入购物车
  Widget _addCart(context) {
    return InkWell(
      onTap: () {
        print("加入购物车");
        CommodityCartPrefs.saveCommodityCart(commodity);
        _updateCommodityBloc(context);
      },
      child: Container(
        alignment: Alignment.center,
        width: ScreenUtil().setWidth(320),
        height: ScreenUtil().setHeight(80),
        color: Colors.green,
        child: Text(
          '加入购物车',
          style: TextStyle(
            color: Colors.white,
            fontSize: ScreenUtil().setSp(28),
          ),
        ),
      ),
    );
  }

  //立即购买
  Widget _nowBuy(context) {
    return InkWell(
      onTap: () {
        print("立即购买");
        CommodityCartPrefs.deleteCommodity(commodity.commodityId);
        _updateCommodityBloc(context);
      },
      child: Container(
        alignment: Alignment.center,
        width: ScreenUtil().setWidth(320),
        color: Colors.redAccent,
        child: Text(
          '立即购买',
          style: TextStyle(
            fontSize: ScreenUtil().setSp(28),
            color: Colors.white,
          ),
        ),
      ),
    );
  }

  //更新bloc
  void _updateCommodityBloc(context) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String listStr = prefs.getString(CommodityCartPrefs.cartListKey);
    BlocProvider.of<CartBloc>(context).add(SetCartEvent(listStr));
  }
}
